﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using MyCompanyName.AbpZeroTemplate.Persons.Authorization;
using MyCompanyName.AbpZeroTemplate.Persons.Dtos;
using MyCompanyName.AbpZeroTemplate.Dto;

namespace MyCompanyName.AbpZeroTemplate.Persons
{
    /// <summary>
    /// 联系人服务实现
    /// </summary>
    [AbpAuthorize(PersonAppPermissions.Person)]
    public class PersonAppService : AbpZeroTemplateAppServiceBase, IPersonAppService
    {
        private readonly IRepository<Person, int> _personRepository;
        private readonly IPersonListExcelExporter _personListExcelExporter;

        private readonly PersonManage _personManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public PersonAppService(IRepository<Person, int> personRepository,
            PersonManage personManage,
            IPersonListExcelExporter personListExcelExporter)
        {
            _personRepository = personRepository;
            _personManage = personManage;
            _personListExcelExporter = personListExcelExporter;
        }

        #region 实体的自定义扩展方法
        private IQueryable<Person> _personRepositoryAsNoTrack => _personRepository.GetAll().AsNoTracking();

        #endregion

        #region 联系人管理

        /// <summary>
        /// 根据查询条件获取联系人分页列表
        /// </summary>
        public async Task<PagedResultDto<PersonListDto>> GetPagedPersonsAsync(GetPersonInput input)
        {
            var query = _personRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var personCount = await query.CountAsync();

            var persons = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var personListDtos = persons.MapTo<List<PersonListDto>>();
            return new PagedResultDto<PersonListDto>(
                personCount,
                personListDtos
            );
        }

        /// <summary>
        /// 通过Id获取联系人信息进行编辑或修改 
        /// </summary>
        public async Task<GetPersonForEditOutput> GetPersonForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetPersonForEditOutput();

            PersonEditDto personEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _personRepository.GetAsync(input.Id.Value);
                personEditDto = entity.MapTo<PersonEditDto>();
            }
            else
            {
                personEditDto = new PersonEditDto();
            }

            output.Person = personEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取联系人ListDto信息
        /// </summary>
        public async Task<PersonListDto> GetPersonByIdAsync(EntityDto<int> input)
        {
            var entity = await _personRepository.GetAsync(input.Id);

            return entity.MapTo<PersonListDto>();
        }

        /// <summary>
        /// 新增或更改联系人
        /// </summary>
        public async Task CreateOrUpdatePersonAsync(CreateOrUpdatePersonInput input)
        {
            if (input.PersonEditDto.Id.HasValue)
            {
                await UpdatePersonAsync(input.PersonEditDto);
            }
            else
            {
                await CreatePersonAsync(input.PersonEditDto);
            }
        }

        /// <summary>
        /// 新增联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_CreatePerson)]
        public virtual async Task<PersonEditDto> CreatePersonAsync(PersonEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<Person>();

            entity = await _personRepository.InsertAsync(entity);
            return entity.MapTo<PersonEditDto>();
        }

        /// <summary>
        /// 编辑联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_EditPerson)]
        public virtual async Task UpdatePersonAsync(PersonEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _personRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _personRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_DeletePerson)]
        public async Task DeletePersonAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _personRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除联系人
        /// </summary>
        [AbpAuthorize(PersonAppPermissions.Person_DeletePerson)]
        public async Task BatchDeletePersonAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _personRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion
        #region 联系人的Excel导出功能


        public async Task<FileDto> GetPersonToExcel()
        {
            var entities = await _personRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<PersonListDto>>();

            var fileDto = _personListExcelExporter.ExportPersonToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
